﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace HotellApplikationASP
{
    public partial class Camping_test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            svgArea.Text = File.ReadAllText(MapPath(@"~\Resources\camp_map.svg"));

            disableDatesArray.Text = "<script>var disableDates =\"" + getDisableDates() + "\"</script>";

        }

        private string getDisableDates()
        {
            var db = new HotellbokningASPEntities();
            var dates = db.RoomOrderDetails.Select(m => new { roomId = m.RoomId, startDate = m.StartDate, endDate = m.EndDate });
            var rooms = db.RoomOrderDetails.GroupBy(c => c.RoomId).ToList();

            string retval = "{ houses: [";
            int i = 0;
            foreach (var room in rooms)
            {
                retval += "{";
                    retval += "\\\"houseId\\\":"+room.Key+",";
                    retval += "\\\"description\\\":\\\"" + room.First().Room.Description+"\\\",";
                    retval += "\\\"costPerNight\\\":" + room.First().Room.CostPerNight + ",";
                    retval += "\\\"roomType\\\":\\\"" + room.First().Room.RoomType1.Description + "\\\",";
                    retval += "\\\"dates\\\": [";
                    foreach (var date in rooms.ElementAt(i))
                    {
                        var dRange = DateRange(date.StartDate, date.EndDate);
                        foreach (var d in dRange)
                        {
                            retval +=  "\\\""+d.ToShortDateString()+"\\\"";
                            if (dRange.Last() != d)
                            {
                                retval += ",";
                            } 
                        }
                        if (rooms.ElementAt(i).Last() != date)
                        {
                            retval += ",";
                        }
                    }
                    i++;
                    retval += "]";
                retval += "}";
                if (rooms.Last() != room)
                {
                    retval += ",";
                }
            }
            retval += "]}";
            return retval;
        }

        private IEnumerable<DateTime> DateRange(DateTime fromDate, DateTime toDate)
        {
            return Enumerable.Range(0, toDate.Subtract(fromDate).Days + 1)
                             .Select(d => fromDate.AddDays(d));
        }
    }
}